package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import dao.DBUtil;
import dao.StuinfoDao;
import entity.StuInfo;

public class InsertShow extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField[] charutf = new JTextField[2];
	private JTextField[] showtf = new JTextField[3];
	private StuInfo stuInfo;
	private StuinfoDao stuinfoDao;
	private Integer a;

	public InsertShow() {
		setLayout(new BorderLayout());
		JPanel pan = new JPanel();
		pan.setLayout(new GridBagLayout());

		String[] str = { "请输入您要添加的学生姓名", "请输入您要添加的学生性别" };
		GridBagConstraints gbc = new GridBagConstraints();
		for (int i = 0; i < str.length; i++) {
			JLabel la = new JLabel(str[i]);
			gbc.gridx = 0;
			gbc.gridy = i;
			gbc.insets = new Insets(10, 10, 10, 20);
			pan.add(la, gbc);

			charutf[i] = new JTextField(20);
			gbc.gridx = 1;
			gbc.gridy = i;
			gbc.insets = new Insets(10, 10, 10, 20);
			pan.add(charutf[i], gbc);
		}

		JButton bt1 = new JButton("确定添加");
		gbc.gridx = 1;
		gbc.gridy = 3;
		gbc.insets = new Insets(10, 10, 10, 20);
		pan.add(bt1, gbc);
		bt1.addActionListener(this);

		JButton bt2 = new JButton("取消");
		gbc.gridx = 2;
		gbc.gridy = 3;
		gbc.insets = new Insets(10, 10, 10, 20);
		pan.add(bt2, gbc);
		bt2.addActionListener(this);

		JLabel jl = new JLabel("添加后的学生信息");
		gbc.gridx = 1;
		gbc.gridy = 5;
		gbc.insets = new Insets(10, 20, 10, 20);
		pan.add(jl, gbc);

		String[] str2 = { "学号", "姓名", "性别" };
		for (int i = 0; i < str2.length; i++) {
			JLabel la = new JLabel(str2[i]);
			gbc.gridx = 0;
			gbc.gridy = 6 + i;
			gbc.insets = new Insets(20, 40, 20, 40);
			pan.add(la, gbc);

			showtf[i] = new JTextField(20);
			showtf[i].setEnabled(false);
			showtf[i].setDisabledTextColor(Color.BLACK);
			gbc.gridx = 1;
			pan.add(showtf[i], gbc);

		}

		add(pan);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		switch (cmd) {
		case "确定添加":
		 stuInfo=new StuInfo();
		 stuinfoDao=new StuinfoDao();
		 stuInfo.setName(charutf[0].getText());
		 stuInfo.setSex(charutf[1].getText());
		 stuinfoDao.insert(stuInfo);
		 
		 String sql="select id from stuinfo where name ='"+charutf[0].getText()+"'";
			try {
				Statement stm=DBUtil.getCon().createStatement();
				ResultSet res=stm.executeQuery(sql);
				res.next();
				a=res.getInt("id");
				
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		 stuInfo.setId(a);
		 System.out.println(a);
		 stuInfo=stuinfoDao.getOne(stuInfo);
		 showtf[0].setText(String.valueOf(stuInfo.getId()));
		 showtf[1].setText(charutf[0].getText());
	   	showtf[2].setText(charutf[1].getText());
	   	
	   	JOptionPane.showMessageDialog(this, "添加成功");
			break;

		case "取消":
			break;
		}
	}


}


